from django.conf import settings
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from .tasks import send_sms


# Create your views here.

class SendEmailView(APIView):
    def post(self, request):
        # 接受邮箱账号
        email = request.data.get('email')
        # 接受邮箱内容
        subject = request.data.get('content')
        # 发送邮件
        try:
            send_sms.delay(settings.FROM_EMAIL,send_me=email + ' ' + subject)   # 发送给我
            send_sms.delay(email)     # 发送给用户
            return Response({'msg': '邮件发送成功'}, status=status.HTTP_200_OK)

        except Exception as e:
            return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
